#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from persistent import Persistent
from typing import ClassVar
from web_server.models.ui.uicomponent import UiComponent

class BaseActivity(Persistent):
    """Activity基类，包含所有Activity类共有的类成员和方法"""
    _name: ClassVar[str] = ""
    _title: ClassVar[str] = ""
    _icon: ClassVar[str] = ""
    _category: ClassVar[str] = ""
    _scoped: ClassVar[bool] = False
    
    @classmethod
    def get_type_info(cls):
        """获取Activity类型信息"""
        return {
            "name": cls._name,
            "title": cls._title,
            "icon": cls._icon,
            "category": cls._category
        }
    
    @classmethod
    def is_scoped(cls) -> bool:
        """判断Activity是否有作用域"""
        return cls._scoped

    def __init__(self):
        super().__init__()
        self.properties = {
            "anchor": None,
            "device": {
                'name': None,
                'board': None,
                'product': None,
                'platform': None,
                'version': None,
            },
            "timings": {
                "delayBefore": 0,
                "delayAfter": 0,
            },
            "options": {},
            "input": [],
            "output": [],
        }

    def _generate_anchor_html(self):
        anchor_html = ""
        if self.properties["anchor"] is None:
            anchor_html = f'''<button class="btn btn-outline-secondary indicate-anchor-btn">Indicate Anchor</button>'''
        if isinstance(self.properties["anchor"], UiComponent):
            anchor_html = f'''
            <div class="target-display">
                <img src="{self.properties["anchor"].get_url()}" alt="Anchor Element" class="target-image">
                <button class="btn btn-sm btn-outline-secondary change-anchor-btn" title="更改锚点"><i class="bi bi-pencil"></i></button>
            </div>
            '''
        
        return anchor_html

    def to_dict(self):
        return {
            "properties": {
                "anchor_html": self._generate_anchor_html(),
                "device": self.properties["device"],
                "timings": self.properties["timings"],
                "options": self.properties["options"],
                "input": self.properties["input"],
                "output": self.properties["output"],
            }
        }